﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ModularFixture.CApMFFixtureDesignerUtils
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EMPAPPSERVICESLib;
using EMPMODELLib;
using EMPTYPELIBRARYLib;
using System;
using System.Collections;
using System.Collections.ObjectModel;
using Tecnomatix.Engineering;
using Tecnomatix.Planning;


namespace DnProcessSimulateCommands.ModularFixture;

internal class CApMFFixtureDesignerUtils
{
  internal const string CONFIGURATION_PROTOTYPE_ID = "ConfigurationPrototypeId";
  internal const string PART_TYPE = "Modfix_ConnectingPart";
  internal const string RAISER_TYPE = "raiser";
  internal const string PROTOTYPE_FIELD = "prototype";
  internal const string LAYOUT_FIELD = "layout";
  internal const string CHILDREN_FIELD = "children";
  internal const string CLASS_DEF_FIELD = "Ex_ClassDef";
  internal const string LAYOUT_CLASS_NAME = "Layout";
  internal const string TOOL_INSTANCE_CLASS_NAME = "PmToolInstance";
  internal const string LOCATION_FIELD = "location";
  internal const string ROTATION_FIELD = "rotation";
  internal const string WEIGHT_FIELD = "weight";
  internal const string VALID_SOLUTION_ATTRIBUTE = "ValidSolution";

  internal static bool DoesObjectExistInEms(int internalId)
  {
    EmpContext empContext;
    empContext.sessionId = TxApplication.ActiveDocument.eMSSessionId;
    EmpObjectClass empObjectClass = new EmpObjectClass();
    EmpObjectKey[] empObjectKeyArray = new EmpObjectKey[1];
    empObjectKeyArray[0].objectId = internalId;
    return empObjectClass.DoObjectsExist(ref empContext, ref empObjectKeyArray)[0];
  }

  internal static ArrayList GetRaisersFromConfiguration(int configurationId)
  {
    ArrayList raiserList = new ArrayList();
    ArrayList raiserPath = new ArrayList();
    CApMFFixtureDesignerUtils.GetRaisersFromConfigurationInternal(configurationId, raiserList, raiserPath);
    return raiserList;
  }

  internal static ArrayList GetIntermediatesFromConfiguration(int configurationId)
  {
    ArrayList intermediateList = new ArrayList();
    ArrayList intermediatePath = new ArrayList();
    CApMFFixtureDesignerUtils.GetIntermediatesFromConfigurationInternal(configurationId, intermediateList, intermediatePath);
    return intermediateList;
  }

  internal static double GetDistanceBetweenPoints(ref TxVector point1, ref TxVector point2)
  {
    TxVector txVector = TxVector.Subtract(point1, point2);
    return Math.Sqrt(TxVector.DotProduct(txVector, txVector));
  }

  internal static TxTransformation GetFixtureUnitOrigin(int configurationId)
  {
    TxTransformation fixtureUnitOrigin = (TxTransformation) null;
    EmpContext empContext;
    empContext.sessionId = TxApplication.ActiveDocument.eMSSessionId;
    EmpFieldInfo field = ((IEmpObject) new EmpObjectClass()).GetField(ref empContext, ref new EmpObjectKey()
    {
      objectId = configurationId
    }, "FixtureUnitOrigin");
    if (field.Value != null)
    {
      double[] numArray = (double[]) field.Value;
      fixtureUnitOrigin = new TxTransformation(new TxVector(numArray[0], numArray[1], numArray[2]), new TxVector(numArray[3], numArray[4], numArray[5]), (TxTransformation.TxRotationType) 1);
    }
    return fixtureUnitOrigin;
  }

  internal static void GetToleranceForConfiguration(
    int configurationId,
    out double minToleranceX,
    out double maxToleranceX,
    out double minToleranceY,
    out double maxToleranceY,
    out double minToleranceZ,
    out double maxToleranceZ)
  {
    EmpContext empContext;
    empContext.sessionId = TxApplication.ActiveDocument.eMSSessionId;
    IEmpNodesServices iempNodesServices = (IEmpNodesServices) new EmpNodesServicesClass();
    string[] strArray = new string[6]
    {
      "MinToleranceX",
      "MaxToleranceX",
      "MinToleranceY",
      "MaxToleranceY",
      "MinToleranceZ",
      "MaxToleranceZ"
    };
    EmpObjectKey[] empObjectKeyArray = new EmpObjectKey[1];
    empObjectKeyArray[0].objectId = configurationId;
    EmpFieldInfo[] fields = iempNodesServices.GetFields(ref empContext, ref empObjectKeyArray, ref strArray);
    minToleranceX = (double) fields[0].Value;
    maxToleranceX = (double) fields[1].Value;
    minToleranceY = (double) fields[2].Value;
    maxToleranceY = (double) fields[3].Value;
    minToleranceZ = (double) fields[4].Value;
    maxToleranceZ = (double) fields[5].Value;
  }

  internal static ArrayList GetPlacementPoints(int fixtureBaseId)
  {
    EmpContext empContext;
    empContext.sessionId = TxApplication.ActiveDocument.eMSSessionId;
    ArrayList placementPoints = (ArrayList) null;
    EmpFieldInfo field = ((IEmpObject) new EmpObjectClass()).GetField(ref empContext, ref new EmpObjectKey()
    {
      objectId = fixtureBaseId
    }, "PlacementPoints");
    if (field.Value != null)
      placementPoints = CApMFPlacementPoint.Parse((string[]) field.Value);
    CApMFFixtureDesignerUtils.UpdateIntermediateUnitsForPlacementPoints(fixtureBaseId, placementPoints);
    return placementPoints;
  }

  internal static CApMFTransformation GetIntermediatePlacementPoint(int unitId)
  {
    CApMFTransformation intermediatePlacementPoint = (CApMFTransformation) null;
    ArrayList field = (ArrayList) new TxEmsGlobalServicesProvider().GetObjectByInternalId(unitId).GetField("IntermediateUnitPlacementPoint");
    if (field != null)
      intermediatePlacementPoint = new CApMFTransformation(Convert.ToDouble(field[0]), Convert.ToDouble(field[1]), Convert.ToDouble(field[2]), Convert.ToDouble(field[3]), Convert.ToDouble(field[4]), Convert.ToDouble(field[5]));
    return intermediatePlacementPoint;
  }

  internal static int GetInternalIdFromExternalId(string externalId)
  {
    return (new TxEmsGlobalServicesProvider().GetObjectByProcessModelId(new TxProcessModelId(externalId)).PlatformServicesProvider as TxEmsServicesProvider).InternalId;
  }

  internal static bool GetPlacementOrigin(int instanceId, ref CApMFTransformation placementOrigin)
  {
    bool placementOrigin1 = false;
    ArrayList field = (ArrayList) new TxEmsGlobalServicesProvider().GetObjectByInternalId(instanceId).GetField("PlacementOrigin");
    if (field != null)
    {
      placementOrigin = new CApMFTransformation(Convert.ToDouble(field[0]), Convert.ToDouble(field[1]), Convert.ToDouble(field[2]), Convert.ToDouble(field[3]), Convert.ToDouble(field[4]), Convert.ToDouble(field[5]));
      placementOrigin1 = true;
    }
    else
      placementOrigin = new CApMFTransformation();
    return placementOrigin1;
  }

  internal static void GetToolPrototypesFromConfiguration(
    TxPlanningObject planningObj,
    ref ArrayList protoList)
  {
    foreach (ITxPlanningObject planningObj1 in (Collection<ITxObject>) (planningObj.GetField("children") as TxObjectList))
      CApMFFixtureDesignerUtils.GetToolPrototypesInternal(planningObj1, ref protoList);
  }

  internal static bool GetPlacementOriginFromConfiguration(
    TxPlanningObject planningObj,
    ref ArrayList placementOriginPath)
  {
    bool placementOriginFound = false;
    foreach (TxPlanningObject planningObj1 in (Collection<ITxObject>) (planningObj.GetField("children") as TxObjectList))
      CApMFFixtureDesignerUtils.GetPlacementOriginInternal(planningObj1, ref placementOriginPath, ref placementOriginFound);
    return placementOriginFound;
  }

  private static void GetPlacementOriginInternal(
    TxPlanningObject planningObj,
    ref ArrayList placementOriginPath,
    ref bool placementOriginFound)
  {
    if (placementOriginFound)
      return;
    TxPlanningObject field = planningObj.GetField("prototype") as TxPlanningObject;
    if (field.IsDerivedFromPlanningType("UnitConfigurationPrototype"))
    {
      placementOriginPath.Add((object) planningObj);
      foreach (TxPlanningObject planningObj1 in (Collection<ITxObject>) (field.GetField("children") as TxObjectList))
        CApMFFixtureDesignerUtils.GetPlacementOriginInternal(planningObj1, ref placementOriginPath, ref placementOriginFound);
      placementOriginPath.Remove((object) planningObj);
    }
    else
    {
      if (!field.IsDerivedFromPlanningType("ToolPrototype") || planningObj.GetField("PlacementOrigin") == null)
        return;
      TxEmsServicesProvider servicesProvider = planningObj.PlatformServicesProvider as TxEmsServicesProvider;
      placementOriginPath.Add((object) servicesProvider.InternalId);
      placementOriginFound = true;
    }
  }

  private static void GetToolPrototypesInternal(
    ITxPlanningObject planningObj,
    ref ArrayList toolPrototyes)
  {
    TxPlanningObject field = planningObj.GetField("prototype") as TxPlanningObject;
    if (field.IsDerivedFromPlanningType("UnitConfigurationPrototype"))
    {
      foreach (ITxPlanningObject planningObj1 in (Collection<ITxObject>) (field.GetField("children") as TxObjectList))
        CApMFFixtureDesignerUtils.GetToolPrototypesInternal(planningObj1, ref toolPrototyes);
    }
    else
    {
      if (!field.IsDerivedFromPlanningType("ToolPrototype"))
        return;
      TxEmsServicesProvider servicesProvider = field.PlatformServicesProvider as TxEmsServicesProvider;
      toolPrototyes.Add((object) servicesProvider.InternalId);
    }
  }

  private static void GetRaisersFromConfigurationInternal(
    int instanceId,
    ArrayList raiserList,
    ArrayList raiserPath)
  {
    EmpContext empContext;
    empContext.sessionId = TxApplication.ActiveDocument.eMSSessionId;
    EmpObjectKey empObjectKey1;
    empObjectKey1.objectId = instanceId;
    EmpToolPrototypeClass toolPrototypeClass = new EmpToolPrototypeClass();
    EmpObjectKey[] children = toolPrototypeClass.GetChildren(ref empContext, ref empObjectKey1);
    for (int index = 0; index < children.Length; ++index)
    {
      EmpObjectKey empObjectKey2 = new EmpToolInstanceClass()[ref empContext, ref children[index]];
      if (toolPrototypeClass.IsKindOf(ref empContext, ref empObjectKey2, "UnitConfigurationPrototype"))
      {
        raiserPath.Add((object) children[index].objectId);
        CApMFFixtureDesignerUtils.GetRaisersFromConfigurationInternal(empObjectKey2.objectId, raiserList, raiserPath);
        raiserPath.RemoveAt(raiserPath.Count - 1);
      }
      else if (toolPrototypeClass.IsKindOf(ref empContext, ref empObjectKey2, "ToolPrototype"))
      {
        EmpFieldInfo field = ((IEmpObject) new EmpObjectClass()).GetField(ref empContext, ref empObjectKey2, "Modfix_ConnectingPart");
        if (field.Value != null && (int) field.Value == 1)
        {
          ArrayList arrayList = (ArrayList) raiserPath.Clone();
          arrayList.Add((object) children[index].objectId);
          raiserList.Add((object) arrayList);
        }
      }
    }
  }

  private static void GetIntermediatesFromConfigurationInternal(
    int prototypeId,
    ArrayList intermediateList,
    ArrayList intermediatePath)
  {
    EmpContext empContext;
    empContext.sessionId = TxApplication.ActiveDocument.eMSSessionId;
    EmpObjectKey empObjectKey1;
    empObjectKey1.objectId = prototypeId;
    EmpToolPrototypeClass toolPrototypeClass = new EmpToolPrototypeClass();
    EmpObjectKey[] children = toolPrototypeClass.GetChildren(ref empContext, ref empObjectKey1);
    for (int index = 0; index < children.Length; ++index)
    {
      EmpObjectKey empObjectKey2 = new EmpToolInstanceClass()[ref empContext, ref children[index]];
      if (toolPrototypeClass.IsKindOf(ref empContext, ref empObjectKey2, "UnitConfigurationPrototype"))
      {
        intermediatePath.Add((object) children[index].objectId);
        CApMFFixtureDesignerUtils.GetIntermediatesFromConfigurationInternal(empObjectKey2.objectId, intermediateList, intermediatePath);
        intermediatePath.RemoveAt(intermediatePath.Count - 1);
      }
      else if (toolPrototypeClass.IsKindOf(ref empContext, ref empObjectKey2, "ToolPrototype") && ((IEmpObject) new EmpObjectClass()).GetField(ref empContext, ref children[index], "IntermediateUnitPlacementPoint").Value != null)
      {
        ArrayList arrayList = (ArrayList) intermediatePath.Clone();
        arrayList.Add((object) children[index].objectId);
        intermediateList.Add((object) arrayList);
      }
    }
  }

  private static void UpdateIntermediateUnitsForPlacementPoints(
    int fixtureBaseId,
    ArrayList placementPoints)
  {
    EmpContext empContext;
    empContext.sessionId = TxApplication.ActiveDocument.eMSSessionId;
    EmpFieldInfo field = ((IEmpObject) new EmpObjectClass()).GetField(ref empContext, ref new EmpObjectKey()
    {
      objectId = fixtureBaseId
    }, "IntermediateUnits");
    if (field.Value == null)
      return;
    string[] strArray = (string[]) field.Value;
    for (int index1 = 0; index1 < placementPoints.Count; ++index1)
    {
      for (int index2 = 0; index2 < strArray.Length; index2 += 2)
      {
        if (((CApMFPlacementPoint) placementPoints[index1]).Name.Equals(strArray[index2]))
        {
          TxEmsServicesProvider servicesProvider = new TxEmsGlobalServicesProvider().GetObjectByProcessModelId(new TxProcessModelId(strArray[index2 + 1])).PlatformServicesProvider as TxEmsServicesProvider;
          ((CApMFPlacementPoint) placementPoints[index1]).IntermediateUnitId = servicesProvider.InternalId;
        }
      }
    }
  }
}
